package com.yantong.controller;

import com.yantong.annotation.Log;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.Video;
import com.yantong.service.VideoService;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 视频管理控制类
 * @Auther: chenhu
 * @Date: 2020/08/03 15:34
 * @Description:
 */
@Controller
@RequestMapping("/sys/video")
public class VideoController extends BaseController {
    private static final String prefix = "system/video";
    @Autowired
    private VideoService videoService;

    /**
     * 视频类型页面
     */
    @RequiresPermissions("sys:video:videoType")
    @GetMapping("/videoType")
    public String videoType(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/videoType";
    }

    /**
     * 视频类型列表
     */
    @ResponseBody
    @RequiresPermissions("sys:video:videoType")
    @GetMapping("/videoTypeList")
    public PageUtils<Video> videoTypeList(String queryItem, Long centreId, String video_name, int offset, int limit) {
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId()) && ValidateUtil.isEmptyObjectOrString(centreId)) {
            centreId=getUserCentreId();
        }
        return videoService.videoTypeList(queryItem, centreId, video_name, offset, limit);
    }

    /**
     * 删除视频类型
     */
    @Log(module = "视频类型管理", operation = "删除视频类型")
    @ResponseBody
    @RequiresPermissions("sys:video:removeVideoType")
    @PostMapping("/removeVideoType")
    public ResultVo removeVideoType(Long video_id) {
        if (video_id == null) {
            return ResultVo.error();
        }
        Video v = new Video();
        v.setStatus("0");
        v.setVideo_id(video_id);
        if (videoService.updateVideoType(v) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 新增视频类型页面
     */
    @RequiresPermissions("sys:video:addVideoType")
    @GetMapping("/addVideoTypePage")
    public String addVideoTypePage() {
        return prefix + "/addVideoTypePage";
    }

    /**
     * 新增视频类型操作
     */
    @Log(module = "视频类型管理", operation = "新增视频类型")
    @ResponseBody
    @RequiresPermissions("sys:video:addVideoType")
    @PostMapping("/addVideoType")
    public ResultVo addVideoType(Video video) {
        if (video == null) {
            return ResultVo.error();
        }
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId()) && ValidateUtil.isEmptyObjectOrString(video.getCentreId())) {
            video.setCentreId(getUserCentreId());
        }
        if (videoService.addVideoType(video) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 更新视频类型页面
     */
    @RequiresPermissions("sys:video:editVideoType")
    @GetMapping("editVideoTypePage/{video_id}")
    public String editVideoTypePage(@PathVariable Long video_id, Model model) {
        model.addAttribute("videoType", videoService.getVideoTypePrimaryKey(video_id));
        return prefix + "/editVideoTypePage";
    }

    /**
     * 更新视频类型操作
     */
    @Log(module = "视频类型管理", operation = "修改视频类型")
    @ResponseBody
    @RequiresPermissions("sys:video:editVideoType")
    @PostMapping("/editVideoType")
    public ResultVo editVideoType(Video video) {
        if (video == null || video.getVideo_id() == null) {
            return ResultVo.error();
        }
        if (videoService.updateVideoType(video) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 视频列表页面
     */
    @RequiresPermissions("sys:video:video")
    @GetMapping("/video")
    public String video(Model model) {
        Long centreId=null;
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())) {
            centreId=getUserCentreId();
        }
        model.addAllAttributes(videoService.getVideoTypeListToMap(centreId));
        return prefix + "/video";
    }

    /**
     * 视频列表
     */
    @ResponseBody
    @RequiresPermissions("sys:video:video")
    @GetMapping("/videoList")
    public PageUtils<Video> videoList(String title,Long centre_id,String video_id, int offset, int limit) {
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId()) && ValidateUtil.isEmptyObjectOrString(centre_id)) {
            centre_id=getUserCentreId();
        }
        return videoService.videoList(title,centre_id,video_id, offset, limit);
    }


    /**
     * 新增视频页面
     */
    @RequiresPermissions("sys:video:addVideo")
    @GetMapping("/addVideoPage")
    public String addVideoPage(Model model) {
        Long centreId=null;
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())) {
            centreId=getUserCentreId();
        }
        model.addAllAttributes(videoService.getVideoTypeListToMap(centreId));
        return prefix + "/addVideoPage";
    }

    /**
     * 视频新增操作
     */
    @Log(module = "视频管理", operation = "新增视频")
    @ResponseBody
    @RequiresPermissions("sys:video:addVideo")
    @PostMapping("/addVideo")
    public ResultVo addVideo(Video video) {
        if (video == null) {
            return ResultVo.error();
        }
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())) {
            video.setCentreId(getUserCentreId());
        }
        if (videoService.addVideo(video) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 更新视频页面
     */
    @RequiresPermissions("sys:video:editVideo")
    @GetMapping("editVideoPage/{id}")
    public String editVideoPage(@PathVariable Long id, Model model) {
        Long centreId=null;
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())) {
            centreId=getUserCentreId();
        }
        model.addAllAttributes(videoService.getVideoTypeListToMap(centreId));
        model.addAttribute("video", videoService.getVideoPrimaryKey(id));
        return prefix + "/editVideoPage";
    }

    /**
     * 更新视频操作
     */
    @Log(module = "视频管理", operation = "修改视频")
    @ResponseBody
    @RequiresPermissions("sys:video:editVideo")
    @PostMapping("/editVideo")
    public ResultVo updateVideo(Video video) {
        if (video == null || video.getId() == null) {
            return ResultVo.error();
        }
        if (ValidateUtil.isNotEmptyObjectOrString(getUserCentreId())) {
            video.setCentreId(getUserCentreId());
        }
        if (videoService.updateVideo(video) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除视频
     */
    @Log(module = "视频管理", operation = "删除类型")
    @ResponseBody
    @RequiresPermissions("sys:video:deleteVideo")
    @PostMapping("/deleteVideo")
    public ResultVo deleteVideo(Long id) {
        if (id == null) {
            return ResultVo.error();
        }
        Video v = new Video();
        v.setStatus("0");
        v.setId(id);
        if (videoService.updateVideo(v) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

}
